package blogic;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import signedUser.Signed;
import android.content.Context;
import android.util.Log;
import dbcontext.DBContext;
import dbmodels.Album;
import dbmodels.Photo;
import dbmodels.User;
import dbtypes.PermisionAlbum;
import dbtypes.PermisionPhoto;

public class BusinessLogic {

	private DBContext db;

	public DBContext getDB() {
		return db;
	}

	public BusinessLogic(Context context) {
		db = new DBContext(context);
	}

	public boolean existAlbum(String name, UUID userid) {
		List<Album> albums = db.getAllAlbums();

		if (albums != null && name != null && userid != null)
			for (Album album : albums)
				if (album.getName().equals(name)
						&& album.getUserid().equals(userid))
					return true;

		return false;
	}

	public boolean existUserName(String name) {
		List<User> users = db.getAllUsers();

		if (users != null && name != null)
			for (User user : users)
				if (user.getName().equals(name))
					return true;

		return false;
	}

	public UUID existUser(String name, String pass) {

		List<User> users = db.getAllUsers();

		if (users != null && name != null && pass != null)
			for (User user : users)
				if (user.getName().equals(name) && user.getPass().equals(pass))
					return user.getId();

		return null;
	}

	public boolean AddPhoto(Photo p) {
		if (p == null || !p.isValid())
			return false;

		try {
			return db.insert(p);
		} catch (Exception e) {
			return false;
		}
	}

	public boolean AddAlbum(Album a) {
		if (a == null || !a.isValid())
			return false;

		try {
			return db.insert(a);
		} catch (Exception e) {
			return false;
		}
	}

	public boolean AddUser(User u) {
		if (u == null || !u.isValid())
			return false;

		try {
			return db.insert(u);
		} catch (Exception e) {
			return false;
		}
	}

	public List<Photo> GetPhotosFromAlbum(UUID albumid, UUID userid) {

		List<Photo> photos = db.getAllPhotos();
		List<Photo> result = new ArrayList<Photo>();

		if (photos != null && albumid != null && userid != null)
			for (Photo photo : photos) {
				Log.d("images", photo.getAlbumid()+" "+albumid);
				if (photo.getAlbumid().equals(albumid) &&
						(Signed.getId().equals(userid) ||
						db.getAlbumById(photo.getAlbumid()).getPermision().equals(PermisionAlbum.All) ||
						(
						db.getAlbumById(photo.getAlbumid()).getPermision().equals(PermisionAlbum.Partial) ||
						photo.getPermision().equals(PermisionPhoto.All
								))))
					result.add(photo);
			}

		return result;
	}

	public List<Album> getAlbums(UUID userId) {
		List<Album> albums = db.getAllAlbums();
		List<Album> result = new ArrayList<Album>();

		if (albums != null && userId != null)
			for (Album album : albums)
				if (album.getUserid().equals(userId)
						|| album.getUserid().equals(Signed.defaultsigned)
						|| !album.getPermision().equals(PermisionAlbum.Nothing))
					result.add(album);

		return result;
	}

	public int NumberOfPhotoInAlbum(UUID albumId) {
		List<Photo> photos = db.getAllPhotos();
		int count = 0;

		if (albumId != null && photos != null)
			for (Photo photo : photos)
				if (photo.getAlbumid().equals(albumId))
					count++;

		return count;
	}

	public String UserName(UUID userid) {
		if (userid == null)
			return "";

		User u = db.getUserById(userid);

		if (u == null)
			return "";

		return u.getName();
	}
	
	public String AlbumName(UUID id){
		if (id == null)
			return "";

		Album a = db.getAlbumById(id);

		if (a == null)
			return "";

		return a.getName();
	}
	
	public boolean deleteAlbum(UUID albumid){
		return db.deleteAlbum(albumid);
	}
	
	public boolean deletePhoto(UUID photoid){
		return db.deletePhoto(photoid);
	}
	
	public boolean updatePhoto(Photo p){
		return db.updatePhoto(p);
	}
	
	public boolean updateAlbum(Album a){
		return db.updateAlbum(a);
	}
	
	public Photo getPhoto(UUID id){
		if(id == null)
			return null;
		
		return db.getPhotoById(id);
	}

}
